package dk.itu.spct;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class TestImageReceiverApp {
	public static void main (String args[]) throws Exception{
		Socket socket;
		InputStream is;
		int serverPort = 45678;
		ServerSocket serverSocket;
		serverSocket = new ServerSocket(serverPort);
		ImageIcon icon = new ImageIcon();
		
		while(true) {
			BufferedImage img = null;
					
			try {
				socket = serverSocket.accept();
				is = socket.getInputStream();
				DataInputStream dis = new DataInputStream(is);
				int len = dis.readInt();
				byte[] byteArray =  new byte[len];
				dis.readFully(byteArray);
				img = ImageIO.read(new ByteArrayInputStream(byteArray));
				dis.close();
				is.close();
			} catch (UnknownHostException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Graphics g = img.createGraphics();
		    g.setColor(Color.RED);
		    g.fillOval(0, 0, 0, 0);
		    
		    icon.setImage(img);
		    JOptionPane.showMessageDialog(null, icon);
		}
	}
}
